package com.rybbaby.tss.front.tssMemberReturnClassRecord.service.imp;

import com.rybbaby.tss.core.utils.SequenceUtil;
import com.rybbaby.tss.core.web.service.imp.BaseServiceImp;
import com.rybbaby.tss.front.tssClasses.dao.ITssClassesDao;
import com.rybbaby.tss.front.tssClasses.entity.TssClasses;
import com.rybbaby.tss.front.tssClassesLessons.dao.ITssClassesLessonsDao;
import com.rybbaby.tss.front.tssClassesLessons.entity.TssClassesLessons;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.dao.ITssMemberAttendClassRecordDao;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.entity.TssMemberAttendClassRecord;
import com.rybbaby.tss.front.tssMemberAttendClassRecord.service.ITssMemberAttendClassRecordService;
import com.rybbaby.tss.front.tssMemberCheckAttendance.dao.ITssMemberCheckAttendanceDao;
import com.rybbaby.tss.front.tssMemberCheckAttendance.entity.TssMemberCheckAttendance;
import com.rybbaby.tss.front.tssMemberPackage.dao.ITssMemberPackageDao;
import com.rybbaby.tss.front.tssMemberPackage.entity.TssMemberPackage;
import com.rybbaby.tss.front.tssMemberPackage.service.ITssMemberPackageService;
import com.rybbaby.tss.front.tssMemberReturnClassRecord.dao.ITssMemberReturnClassRecordDao;
import com.rybbaby.tss.front.tssMemberReturnClassRecord.entity.TssMemberReturnClassRecord;
import com.rybbaby.tss.front.tssMemberReturnClassRecord.service.ITssMemberReturnClassRecordService;
import com.sun.xml.internal.bind.v2.runtime.unmarshaller.XsiNilLoader;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author 雷春琼
 * @description 会员退班记录表数据库操作服务实现
 * @createDate 2017-08-24 下午2:45:23
 */
@Service
@Transactional
public class TssMemberReturnClassRecordServiceImp extends BaseServiceImp<TssMemberReturnClassRecord> implements ITssMemberReturnClassRecordService {

    @Autowired
    private ITssMemberAttendClassRecordService tssMemberAttendClassRecordService;
    @Autowired
    private ITssClassesLessonsDao tssClassesLessonsDao;
    @Autowired
    private ITssMemberReturnClassRecordDao dao;
    @Autowired
    private ITssMemberAttendClassRecordDao attendClassRecordDao;
    @Autowired
    private ITssMemberCheckAttendanceDao attendanceDao;
    @Autowired
    private ITssMemberPackageService packageService;

    @Override
    public Boolean saveEntity(TssMemberReturnClassRecord entity, String targetdataId, String memberPackageId, String memberid, String adminUserId, boolean isSaveReturnRecord) throws Exception {
        boolean bool = true;
        //把未上课的改为已退班
        String[] lessonArr = targetdataId.split(",");
        String lessonIds = StringUtils.join(lessonArr, "','");
        List<TssMemberAttendClassRecord> attendClassRecordsList = attendClassRecordDao.getAttendClassListBylessonIdsAndMemberId(lessonIds, memberid);
        List<TssMemberReturnClassRecord> returnClassRecordList = new ArrayList<>();
        List<TssMemberCheckAttendance> checkAttendanceList = new ArrayList<>();
        TssMemberPackage memberPackage = packageService.get(memberPackageId);
        boolean isNotTransit = memberPackage.getType() == 1 ? true : false;
        Integer surplusClassHour = 0;
        Integer surplusFormalClassHour = 0;
        Integer surplusPresentClassHour = 0;
        Integer selectedClassHour = 0;
        Integer unusedClassHour = 0;
        Integer positiveUnusedClassHour = 0;
        Integer giftUnusedClassHour = 0;
        //是否是过度会员课时包
        if(isNotTransit){
            surplusClassHour = memberPackage.getSurplusClassHour();
            surplusFormalClassHour = memberPackage.getSurplusFormalClassHour();
            surplusPresentClassHour = memberPackage.getSurplusPresentClassHour();
            selectedClassHour = memberPackage.getSelectedClassHour();
            unusedClassHour = memberPackage.getUnusedClassHour();
            positiveUnusedClassHour = memberPackage.getPositiveUnusedClassHour();
            giftUnusedClassHour = memberPackage.getGiftUnusedClassHour();
        }else {
            surplusClassHour = memberPackage.getSurplusAttendanceDay();
            surplusFormalClassHour = memberPackage.getSurplusFormalAttendanceDay();
            surplusPresentClassHour = memberPackage.getSurplusPresentAttendanceDay();
            selectedClassHour = memberPackage.getSelectedAttendanceDay();
            unusedClassHour = memberPackage.getUnusedAttendanceDay();
            positiveUnusedClassHour = memberPackage.getPositiveUnusedAttendanceDay();
            giftUnusedClassHour = memberPackage.getGiftUnusedAttendanceDay();
        }
        for (TssMemberAttendClassRecord attendClassRecord : attendClassRecordsList) {
            Integer formalClass = attendClassRecord.getFormalClass();
            Integer giftClass = attendClassRecord.getGiftClass();
            if (formalClass != null) {
                surplusClassHour += formalClass;
                surplusFormalClassHour += formalClass;
                unusedClassHour -= formalClass;
                positiveUnusedClassHour -= formalClass;
                selectedClassHour -= formalClass;
            }
            if (giftClass != null) {
                surplusClassHour += giftClass;
                surplusPresentClassHour += giftClass;
                unusedClassHour -= giftClass;
                giftUnusedClassHour -= giftClass;
                selectedClassHour -= giftClass;
            }
            //将报班记录改为退班
            attendClassRecord.setStatus(3);
            attendClassRecord.setIsReturnSection(true);
            //将考勤记录逻辑删除
            TssMemberCheckAttendance attendance = attendanceDao.findListBySql("SELECT * from tss_member_check_attendance WHERE attendClassId = '" + attendClassRecord.getId() + "'", TssMemberCheckAttendance.class, null).get(0);
            attendance.setUpdateTime(new Timestamp(System.currentTimeMillis()));
            attendance.setIsDelete(1);
            attendance.setAdminUserId(adminUserId);
            checkAttendanceList.add(attendance);
            attendClassRecord = attendClassRecordDao.get(TssMemberAttendClassRecord.class, attendClassRecord.getId());
            //生成退班记录
            if (isSaveReturnRecord) {
                TssMemberReturnClassRecord returnClassRecord = new TssMemberReturnClassRecord();
                returnClassRecord.setCreateTime(new Timestamp(System.currentTimeMillis()));
                returnClassRecord.setAttendClassId(attendClassRecord.getId());
                returnClassRecord.setMemberId(memberid);
                returnClassRecord.setReturnType(1);
                returnClassRecord.setVersion(0);
                returnClassRecordList.add(returnClassRecord);
            }
        }
        attendClassRecordDao.batchUpdate(attendClassRecordsList);
        attendanceDao.batchUpdate(checkAttendanceList);
        if (isSaveReturnRecord) {
            dao.hibernateBatchSave(returnClassRecordList);
        }
        //将课时包更新
        if(isNotTransit){
            memberPackage.setSurplusClassHour(surplusClassHour);
            memberPackage.setSurplusFormalClassHour(surplusFormalClassHour);
            memberPackage.setSurplusPresentClassHour(surplusPresentClassHour);
            memberPackage.setSelectedClassHour(selectedClassHour);
            memberPackage.setUnusedClassHour(unusedClassHour);
            memberPackage.setPositiveUnusedClassHour(positiveUnusedClassHour);
            memberPackage.setGiftUnusedClassHour((giftUnusedClassHour == null ? 0 : giftUnusedClassHour));
        }else {
            memberPackage.setSurplusAttendanceDay(surplusClassHour);
            memberPackage.setSurplusFormalAttendanceDay(surplusFormalClassHour);
            memberPackage.setSurplusPresentAttendanceDay(surplusPresentClassHour);
            memberPackage.setSelectedAttendanceDay(selectedClassHour);
            memberPackage.setUnusedAttendanceDay(unusedClassHour);
            memberPackage.setPositiveUnusedAttendanceDay(positiveUnusedClassHour);
            memberPackage.setGiftUnusedAttendanceDay((giftUnusedClassHour == null ? 0 : giftUnusedClassHour));
        }
        memberPackage.setUpdateTime(new Timestamp(System.currentTimeMillis()));
        memberPackage.setAdminUserId(adminUserId);
        packageService.updateEntitiy(memberPackage);
        return bool;
    }
}
